/*
 * @Author: 葛文杰
 * @Date: 2021/2/19 10:16
 * @Description: 常用方法封装
 */

const { ENUMERATION_TYPE_ARRAY } = require('./common');
const EnumerationManageProxy = require('../proxy/enumerationManageProxy');
const ENUMERATION_TYPE_MAP = new Map(); // 枚举类型缓存
const ENUMERATION_INFO_MAP = new Map(); // 枚举信息缓存

/**
 * @desc 判断参数是否为空, 包括null, undefined, [], '', {}
 * @param {object} obj 需判断的对象
 */
const isEmpty = (obj) => {
    let empty = false;
    if (obj === null || obj === undefined) {
        empty = true;
    } else if ((isArray(obj) || isString(obj)) && obj.length === 0) {
        empty = true;
    } else if (isObject(obj)) {
        let hasProp = false;
        for (let prop in obj) {
            if (prop) {
                hasProp = true;
                break;
            }
        }
        if (!hasProp) {
            empty = true;
        }
    }
    return empty;
};

/**
 * 是否为数组
 * @param obj
 * @return {boolean}
 */
const isArray = (obj) => {
    return _getClass(obj)
      .toLowerCase() === 'array';
};

/**
 * 是否为字符串
 * @param obj
 * @return {boolean}
 */
const isString = (obj) => {
    return _getClass(obj)
      .toLowerCase() === 'string';
};

/**
 * 是否为对象
 * @param obj
 * @return {boolean}
 */
const isObject = (obj) => {
    return _getClass(obj)
      .toLowerCase() === 'object';
};

/**
 * 获取对象类型
 * @param object
 * @return {string}
 * @private
 */
const _getClass = (object) => {
    return Object.prototype.toString.call(object)
      .match(/^\[object\s(.*)\]$/)[1];
};

/**
 * 封装表格数据
 */
const buildTableData = (tableData, total, pageNo, pageSize) => {
    return {
        tableData,
        total,
        pageNo,
        pageSize
    };
};

/**
 * 获取枚举数据类型
 */
const getEnumerationTypeName = (enumerationValue) => {
    let enumerationLabel;
    if (ENUMERATION_TYPE_MAP.has(enumerationValue)) {
        enumerationLabel = ENUMERATION_TYPE_MAP.get(enumerationValue);
        return enumerationLabel;
    }
    for (let { value, label } of ENUMERATION_TYPE_ARRAY) {
        ENUMERATION_TYPE_MAP.set(value, label);
    }
    return ENUMERATION_TYPE_MAP.get(enumerationValue);
};

/**
 * 获取字典名称、值、id，通过id
 * @param conn mysql链接
 * @param enumerationId 枚举id
 * @param enumerationField 需要返回的枚举字段值
 */
const getEnumerationInfoByIdAndType = async (conn, enumerationId, enumerationField) => {
    let enumerationInfo = getEnumerationInfoMapById(enumerationId);
    if (isEmpty(enumerationInfo)) {
        let enumerationArray = await EnumerationManageProxy.getEnumerationInfoById(conn, enumerationId);
        enumerationInfo = enumerationArray[0] || {};
        setEnumerationInfoMapById(enumerationId, enumerationInfo);
    }
    return enumerationInfo[enumerationField];
};

/**
 * 获取枚举数据信息
 */
const getEnumerationInfoMapById = (enumerationId) => {
    let key = `${enumerationId}`;
    let enumerationInfo = {};
    if (ENUMERATION_INFO_MAP.has(key)) {
        enumerationInfo = ENUMERATION_INFO_MAP.get(key);
    }
    return enumerationInfo;
};

/**
 * 设置枚举数据信息
 */
const setEnumerationInfoMapById = (enumerationId, enumerationInfo) => {
    let key = `${enumerationId}`;
    ENUMERATION_INFO_MAP.set(key, enumerationInfo);
};

module.exports = {
    isEmpty,
    buildTableData,
    getEnumerationInfoByIdAndType,
    getEnumerationTypeName,
    getEnumerationInfoMapById,
    setEnumerationInfoMapById
};

